package net.realsee.service.wcs.work.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.work.WcsJobPool;
import net.realsee.domain.wcs.work.bo.WcsJobPoolBo;
import net.realsee.domain.wcs.work.vo.WcsJobPoolVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.work.WcsJobPoolMapper;
import net.realsee.service.wcs.work.IWcsJobPoolService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 作业池基础数据Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsJobPoolServiceImpl extends ServiceImpl<WcsJobPoolMapper, WcsJobPool> implements IWcsJobPoolService {

    private final WcsJobPoolMapper baseMapper;

    /**
     * 查询作业池基础数据
     */
    @Override
    public WcsJobPoolVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询作业池基础数据列表
     */
    @Override
    public TableDataInfo<WcsJobPoolVo> selectVoPage(WcsJobPoolBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsJobPool> lqw = buildVoQueryWrapper(bo);
        Page<WcsJobPoolVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询作业池基础数据列表
     */
    @Override
    public List<WcsJobPoolVo> selectVoList(WcsJobPoolBo bo) {
        LambdaQueryWrapper<WcsJobPool> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsJobPool> buildVoQueryWrapper(WcsJobPoolBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsJobPool> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsJobPool::getId, bo.getId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), WcsJobPool::getName, bo.getName());
        lqw.eq(bo.getMaxThreads() != null, WcsJobPool::getMaxThreads, bo.getMaxThreads());
        lqw.eq(bo.getPriority() != null, WcsJobPool::getPriority, bo.getPriority());
        lqw.like(StringUtils.isNotBlank(bo.getExecutorName()), WcsJobPool::getExecutorName, bo.getExecutorName());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsJobPool::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsJobPool::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsJobPool::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsJobPool::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsJobPool::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsJobPool::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsJobPool::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsJobPool::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsJobPool::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsJobPool::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsJobPool::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsJobPool::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增作业池基础数据
     */
    @Override
    public Boolean insertByBo(WcsJobPoolBo bo) {
        WcsJobPool add = BeanUtil.toBean(bo, WcsJobPool.class);
        return this.save(add);
    }

    /**
     * 修改作业池基础数据
     */
    @Override
    public Boolean updateByBo(WcsJobPoolBo bo) {
        WcsJobPool update = BeanUtil.toBean(bo, WcsJobPool.class);
        return this.updateById(update);
    }

    /**
     * 批量删除作业池基础数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
